/**
 * FireFly (2011)
 */
package com.firefly.kernel.plugins.bootstrap.basic;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Properties;

import com.firefly.kernel.plugins.bootstrap.AbstractBootstrap;
import com.firefly.kernel.plugins.config.property.PropertyKernelConfig;
import com.firefly.kernel.spi.config.IKernelConfig;

/**
 * 引导核心
 * 
 * @version 1.0 2011-1-2
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since FireFly 1.0
 */
public class BasicBootstrap extends AbstractBootstrap {
	/**
	 * 构造函数
	 */
	public BasicBootstrap() {
		Properties props = getConfigProperties();
		if (props == null)
			props = getSystemProperties();
		final PropertyKernelConfig config = new PropertyKernelConfig(props);
		PrivilegedAction<Object> action = new PrivilegedAction<Object>() {
			public Object run() {
				setConfig(config);
				return null;
			}
		};
		AccessController.doPrivileged(action);
	}

	/**
	 * 构造函数
	 * 
	 * @param config
	 *            核心配置
	 * @throws Exception
	 *             异常
	 */
	public BasicBootstrap(final IKernelConfig config) throws Exception {
		PrivilegedAction<Object> action = new PrivilegedAction<Object>() {
			public Object run() {
				setConfig(config);
				return null;
			}
		};
		AccessController.doPrivileged(action);
	}

	/**
	 * 获取自定义配置属性
	 * 
	 * @return 自定义配置属性
	 */
	protected Properties getConfigProperties() {
		return null;
	}

	private Properties getSystemProperties() {
		if (System.getSecurityManager() == null)
			return System.getProperties();
		else {
			return AccessController.doPrivileged(GetSystemProperties.instance);
		}
	}

	private static class GetSystemProperties implements PrivilegedAction<Properties> {
		private static GetSystemProperties instance = new GetSystemProperties();

		public Properties run() {
			return System.getProperties();
		}
	}

	/**
	 * 命令行引导核心
	 * 
	 * @param args
	 *            命令行参数
	 * @throws Exception
	 *             异常
	 */
	public static void main(String[] args) throws Exception {
		BasicBootstrap bootstrap = new BasicBootstrap();
		bootstrap.run();
	}
}
